package cn.zhangjie.product.service.impl;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.service.imp.BaiduAiAuditServiceImpl;
import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.pet.domain.Pet;
import cn.zhangjie.pet.domain.PetDetail;
import cn.zhangjie.product.domain.Product;
import cn.zhangjie.product.domain.ProductDetail;
import cn.zhangjie.product.mapper.ProductDetailMapper;
import cn.zhangjie.product.mapper.ProductMapper;
import cn.zhangjie.product.service.IProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2022-08-11 23:27:38
 */
@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BasicServiceImpl<Product> implements IProductService  {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;

    @Override
    public void add(Product product) {
        Boolean aBoolean = baiduAiAuditService.textAudit(product.getName());
        if(aBoolean){
            // 先添加pet，返回自增的主键
            productMapper.save(product);

            // 再添加pet_datail
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProduct_id(product.getId());
            productDetailMapper.save(productDetail);
        }
        else {
            throw new BusinssException("违规名字不能有！");
        }

    }
    @Override
    public void update(Product product) {
        productMapper.update(product);
        productDetailMapper.update(product.getProductDetail());
    }
    @Override
    public void del(Long id) {

        super.del(id);
        // 详情
        productDetailMapper.removeByPetId(id);
    }


    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        productMapper.onsale(ids,date);
    }

    @Override
    public void offsale(List<Long> ids) {
        Date date = new Date();
        productMapper.offsale(ids,date);
    }
}
